import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import 'package:toys_flutter/api/app.dart';
import 'package:toys_flutter/components/MyIconButton.dart';
import 'package:toys_flutter/components/RefreshableListView.dart';
import 'package:toys_flutter/components/user/BlockData.dart';
import 'package:toys_flutter/components/user/BlockMoney.dart';
import 'package:toys_flutter/components/user/CharismaWealth.dart';
import 'package:toys_flutter/components/user/MyMenu.dart';
import 'package:toys_flutter/components/user/UserInfoTop.dart';
import 'package:toys_flutter/pages/user/UserSetting.dart';
import 'package:toys_flutter/utils/index.dart';

class UserPage extends StatefulWidget {
  const UserPage({super.key});

  @override
  _State createState() => _State();
}

class _State extends State<UserPage>  {
  Map<String, dynamic>? userData; // 存储用户数据的状态变量

  @override
  void initState() {
    super.initState();
    getData();
  }

  void getData() async {
    var res = await Api.getCenter1();
    if (res != null && res['code'] == 1) {
      setState(() {
        userData = res['data'];
      });
    } else {
      showDialog(
        context: context,
        builder: (BuildContext context) {
          return const LoginExpiredDialog();
        },
      );
    }
  }

  @override
  void dispose() {
    super.dispose();
  }

  // 模拟数据刷新
  Future<void> _refreshData() async {
    try {
      getData();
    } catch (e) {
      print('Error: $e');
      TDToast.showIconText('数据刷新失败',
          icon: TDIcons.error,
          direction: IconTextDirection.vertical,
          context: context);
    }
  }

  @override
  Widget build(BuildContext context) {
    const Color colorMain = Color.fromRGBO(181, 245, 236, 1);

    return Scaffold(
      appBar: AppBar(
        backgroundColor: colorMain,
        actions: [
          Padding(
            padding: const EdgeInsets.only(right: 15),
            child: MyIconButton(
                icon: Icons.settings,
                onPressed: () {
                  // 跳转到 UserSetting
                  Get.to(const UserSetting());
                }),
          ),
        ],
      ),
      body: Container(
          decoration: const BoxDecoration(
            gradient: LinearGradient(
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
              colors: [
                colorMain,
                Colors.white,
                Colors.white,
                Colors.white,
              ],
            ),
          ),
          child: RefreshableListView(
            enableLoadMore: false,
            onRefresh: _refreshData,
            children: [
              UserInfoTop(userData: userData),
              CharismaWealth(userData: userData),
              BlockData(userData: userData),
              const SizedBox(height: 20),
              BlockMoney(userData: userData),
              const MyMenu(),
              const SizedBox(height: 30),
            ],
          )),
    );
  }
}
